<?php
/**
 * Create a material on the flow popup
 * This is a simple view to create basic material
 * from a sales object.
 */

use Fox\MaterialManagement\Material\Type;
use Fox\MaterialManagement\Material;
use Fox\Html\Screen;
$xview = Xview::instance();
$hierarchy = $xview->val("hierarchy");
$inquiryid = $xview->val("inquiryid");
$callback = "ReloadFromObject('eSelectItem','pSelectMaterials','?mod=inquiry&act=materials&sac=add&inquiryid=$inquiryid')";

$screen = new Screen("pAddMaterial","create",null,true,$callback,0);
$block = new Fox\Html\Block("bAddMaterial");
$block->add(new Fox\Html\Input(array("material_label","label"),true));
$block->add(new Fox\Html\Input("hierarchy",true,$hierarchy))->hidden(true);
$screen->add($block);
if(isset($_POST["label"]))
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		try {
			$material = new Material();
			$inputs['label'] = $inputs['label'];
			$inputs['type'] = Material::TYPE_STANDARD;
			$inputs['active'] = 1;
			$material->setFields($inputs);
			$material->create();
			echo Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$inputs['label']));
			exit;
		}
		catch (Fox\Object\Exception\Save $e)
		{
			Fox\Core\Errors::pushMessage($e);
		}
	}
}
$popup = new Fox\Popup("pAddMaterial",Msg(Material::BUNLDE_NAME,18),$screen,null,null,500);
echo $popup->Display();